
const { Server } = require("socket.io");
const { instrument } = require("@socket.io/admin-ui");
const chartServer = require('./chart')

const userMap = new Map()

const io = new Server({
    cors:{
        origin:['http://localhost:3000','https://admin.socket.io'],
        credentials:true
    }
})



io.on("connection", (socket) => {
    console.log('connection:',socket.id);
    socket.on('disconnect',(reason)=>{
        console.log('disconnect',reason);
        userMap.delete(socket.id)
    })
    socket.on('user_config',data=>{
        userMap.set(socket.id,data)
    })
});
io.on('new_namespace',namespace=>{
    console.log('new_namespace:',namespace.name);
})
io.on('message',socket=>{
    console.log('message');
})
io.use((socket,next)=>{
    socket.user = userMap.get(socket.id)
    next()
})
chartServer(io)

module.exports = httpServer =>{
    io.attach(httpServer)
    // instrument(io,{
    //     auth:false
    // })
}
